<template>
  <ContentWrap>
      <!-- 搜索工作栏 -->
      <el-form
        class="-mb-15px"
        :model="queryParams"
        ref="queryFormRef"
        :inline="true"
        label-width="68px"
      >
      <el-form-item label="型号" prop="type">
          <el-input
            v-model="queryParams.type"
            placeholder="请输入型号"
            clearable
            class="!w-240px"
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <!-- <el-form-item label="织板编码" prop="code">
          <el-input
            v-model="queryParams.code"
            placeholder="请输入织板编码"
            clearable
            class="!w-240px"
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item label="织板规格" prop="specification">
          <el-input
            v-model="queryParams.specification"
            placeholder="请输入织板规格"
            clearable
            class="!w-240px"
            @keyup.enter="handleQuery"
          />
        </el-form-item> -->
        <el-form-item>
          <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
          <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
          <el-button
                type="warning"
                plain
                @click="handleImport"
              >
                <Icon icon="ep:upload" /> 导入
              </el-button>
          <el-button
            type="success"
            plain
            @click="handleExport"
            :loading="exportLoading"
            v-hasPermi="['system:post:export']"
          >
            <Icon icon="ep:download" class="mr-5px" /> 下载模板
          </el-button>
        </el-form-item>
      </el-form>
    </ContentWrap>
  <!-- 主列表 -->
  <el-row justify="space-between">
  <el-col :xl="10" :lg="10" :md="24" :sm="24" :xs="24">
  <ContentWrap>
      <el-table v-loading="loading" :data="list" @row-click="getList2">
        <el-table-column type="index" label="序号" align="center" width="55" />
        <el-table-column label="型号" align="center" prop="type" />
        <!-- <el-table-column label="织板编码" align="center" prop="code" />
        <el-table-column label="织板规格" align="center" prop="specification" /> -->
        <!-- <el-table-column label="操作" align="center" width="160">
          <template #default="scope">
            <div class="flex items-center justify-center">
              <el-button
                type="primary"
                link
                @click="openDetail(scope.row.receiptCode)"
              >查看
              </el-button>
            </div>
          </template>
        </el-table-column> -->
      </el-table>
      <!-- 分页 -->
      <Pagination
        :total="total"
        v-model:page="queryParams.pageNo"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
      />
    
    </ContentWrap>
  </el-col>

  <!-- 子表 -->
  <el-col :xl="14" :lg="14" :md="24" :sm="24" :xs="24" class="">
  <ContentWrap>
      <el-table v-loading="loading" :data="list2">
        <el-table-column type="index" label="序号" align="center" width="55" />
        <el-table-column label="型号" align="center" prop="type" />
        <el-table-column label="织板编码" align="center" prop="code" />
        <el-table-column label="织板规格" align="center" prop="specification" />
        <el-table-column label="操作" align="center" width="160">
          <template #default="scope">
            <div class="flex items-center justify-center">
              <el-button
              v-hasPermi="['system:menu:delete']"
              link
              type="danger"
              @click="handleDelete(scope.row.id,scope.row)"
            >
              删除
            </el-button>
            </div>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <Pagination
        :total="total2"
        v-model:page="queryParams.pageNo"
        v-model:limit="queryParams.pageSize"
        @pagination="getList2"
      />
    
    </ContentWrap>
  </el-col>
</el-row>
      <!-- 导入对话框 -->
    <BOMImportForm ref="importFormRef" @success="getList" />
    <!-- 查看详情 -->
    <!-- <BOMDetail ref="formRef" @success="getList" /> -->
  </template>
  
<style lang="scss" scoped>
.hide{
  display: none;
}
</style>

  <script lang="ts" setup>
  // import { DICT_TYPE } from '@/utils/dict'
  import download from '@/utils/download'
  import BOMImportForm from './BOMImportForm.vue'
  // import BOMDetail from './BOMDetail.vue'
  import * as BOMApi from '@/api/hcxly/BOM/Index'
  
  defineOptions({ name: 'BOM' })
  
  const message = useMessage() // 消息弹窗
  const { t } = useI18n() // 国际化
  
  const loading = ref(true) // 列表的加载中
  const total = ref(0) // 列表的总页数
  const total2 = ref(0) // 列表的总页数
  const list = ref([]) // 主列表的数据
  const list2 = ref([]) // 子列表的数据
  const queryParams = reactive({
    pageNo: 1,
    pageSize: 10,
    // id: undefined,
    // name: undefined,
    type: ''
  })
  const queryFormRef = ref() // 搜索的表单
  const exportLoading = ref(false) // 导出的加载中
  
  /** 查询主列表 */
  const getList = async () => {
    loading.value = true
    try {
      const data = await BOMApi.getBOM(queryParams)
      // console.log(data)
      list.value = data.list
      total.value = data.total
    } finally {
      loading.value = false
    }
  }

    /** 查询子列表 */
    const getList2 = async (row) => {
      console.log(row.type)
    loading.value = true
    try {
      const data = await BOMApi.getBOMDetail(row.type)
      list2.value = data.list
      total2.value = data.total
    } finally {
      loading.value = false
    }
  }
  
  /** 搜索按钮操作 */
  const handleQuery = () => {
    queryParams.pageNo = 1
    getList()
  }
  
  /** 重置按钮操作 */
  const resetQuery = () => {
    queryFormRef.value.resetFields()
    handleQuery()
  }
  
  /** 导入 */
  const importFormRef = ref()
  const handleImport = () => {
    importFormRef.value.open()
  }
  
  /** 导出按钮操作 */
  const handleExport = async () => {
    try {
      // 导出的二次确认
      await message.exportConfirm()
      // 发起导出
      exportLoading.value = true
      const data = await BOMApi.exportBOM(queryParams)
      download.excel(data, 'BOM导入模板.xls')
    } catch {
    } finally {
      exportLoading.value = false
    }
  }
  
  /** 初始化 **/
  onMounted(() => {
    getList()
  })
  
  /** 删除按钮操作 */
  const handleDelete = async (id,row) => {
    console.log(row)
    try {
      // 删除的二次确认
      await message.delConfirm()
      // 发起删除
      await BOMApi.deleteBOM(id)
      message.success(t('common.delSuccess'))
      // 刷新列表
      await getList2(row)
    } catch {}
  }

  /** 查看详情操作 */
  // const formRef = ref()
  // const openDetail = (id: string) => {
  //   console.log(id)
  //   formRef.value.open(id)
  // }
  </script>
